<template>
	<view>
		
		<uni-forms :modelValue="form" label-width="80px">
				<uni-forms-item v-if="user.user.password" label="旧密码">
					<uni-easyinput type="password" v-model="form.oldpassword" placeholder="请输入旧密码" />
				</uni-forms-item>
				<uni-forms-item label="新密码">
					<uni-easyinput type="password" v-model="form.newpassword" placeholder="请输入新密码" />
				</uni-forms-item>
				<uni-forms-item label="确认密码">
					<uni-easyinput type="password" v-model="form.newpassword2" placeholder="请输入确认密码" />
				</uni-forms-item>
				<uni-forms-item>
					<view style="display: flex;">
						<button type="primary" @click="submit" style="width: 116rpx; font-size: 30rpx; margin-right: -150rpx;">提交</button>
						<button type="warn" @click="chongzhi" style="width: 116rpx; font-size: 30rpx; margin-right: -150rpx;">重置</button>
						<button type="primary" @click="fanhui" style="width: 116rpx; font-size: 30rpx; margin-right: 10rpx;">返回</button>
					</view>
				</uni-forms-item>
			</uni-forms>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue';
	import { onLoad,onShow } from '@dcloudio/uni-app';
	import {
		http
	} from '@/config/request.js'
	import "@/config/request.js";
	import { useuser } from '@/stores/user.js';
	const user = useuser();
	const form = ref({
		oldpassword: '',
		newpassword: '',
		newpassword2: ''
	})
	const instermima =async () => {
		const res = await http({
			url: '/user/updatepassword',
			method: 'POST',
			data:{
				oldpassword:form.value.oldpassword,
				password:form.value.newpassword,
				id:user.user.id
			},	
		})
		if(res.code==20011){
			
				uni.showToast({
					title: '设置成功,请重新登录',
					icon: 'none',
					duration: 1000
				})
				uni.setStorageSync('user', null)
				setTimeout(() => {
					uni.reLaunch({
						url: '/pages/login/index',
					})
				}, 2000);
				
			}else if(res.code==20040){
				uni.showToast({
					title: '旧密码错误',
					icon: 'none'
				})
				chongzhi();
			}else{
				uni.showToast({
					title: '设置失败',
					icon: 'none'
				})
			}
	}
	const submit = () => {
		if(user.user.password!=null && user.user.password!=''){
			if (form.value.oldpassword == '') {
				uni.showToast({
					title: '请输入旧密码',
					icon: 'none'
				})
				return
			}
		}
		if (form.value.newpassword == '') {
			uni.showToast({
				title: '请输入新密码',
				icon: 'none'
			})
			return
		}
		if (form.value.newpassword2 == '') {
			uni.showToast({
				title: '请输入确认密码',
				icon: 'none'
			})
			return
		}
		if (form.value.newpassword.length < 6) {
			uni.showToast({
				title: '密码长度不能小于6位',
				icon: 'none'
			})
			return
		}
		if (form.value.newpassword2.length < 6) {
			uni.showToast({
				title: '密码长度不能小于6位',
				icon: 'none'
			})
			return
		}
		
		if (form.value.newpassword != form.value.newpassword2) {
			uni.showToast({
				title: '两次密码不一致',
				icon: 'none'
			})
			return
		}
		instermima();
	}
	const chongzhi = () => {
		form.value.oldpassword = ''
		form.value.newpassword = ''
		form.value.newpassword2 = ''
	}
	const fanhui = () => {
		uni.navigateBack()
	}
	onShow(() => {
		if(!user.user.password){
			uni.showToast({
					title: '您还没有设置密码，请先设置密码',
					icon: 'none'
				})
		}
	});
	
</script>

<style>
	       
</style>
